otel: replace cobraotel with native lifecycle management#3108
Conversation
| func RegisterCommonFlags(cmd *cobra.Command) { | ||
| otel := cobraotel.New("spicedb") | ||
| otel.RegisterFlags(cmd.Flags()) | ||
| f := cmd.Flags() |
There was a problem hiding this comment.
Thanks, glad it reads better there
There was a problem hiding this comment.
The code in RegisterOTelFlags looks awfully similar :)
I think this code can be deleted. None of the other commands (other than spicedb serve) use the otel flags.
EDIT: i've gone ahead and deleted it.
| cmd.SetContext(context.Background()) | ||
| require.NoError(t, cmd.Flags().Set("otel-provider", "otlpgrpc")) | ||
| require.NoError(t, cmd.Flags().Set("otel-endpoint", "localhost:4317")) | ||
| require.NoError(t, cmd.Flags().Set("otel-insecure", "true")) |
There was a problem hiding this comment.
I generally like the tests. Can we add a test or two that establishes 1. that you can use the otel environment variables to configure flags that aren't set and 2. which value overrides which if you declare both?
There was a problem hiding this comment.
Done, added TestOTelConfig_EnvVarConfiguresUnsetFlag and TestOTelConfig_ExplicitFlagOverridesEnvVar to the integration test file. First one verifies the SDK picks up OTEL_EXPORTER_OTLP_ENDPOINT when the endpoint flag is not explicitly set, second one documents that an explicit flag value takes precedence over the env var.
There was a problem hiding this comment.
I removed these tests because they weren't really asserting anything useful
miparnisari
left a comment
There was a problem hiding this comment.
Would it be possible to add a new struct within
spicedb/pkg/cmd/server/server.go
Line 63 in ee7c9a7
called otelConfig or something like that, so that anyone doing server.NewConfigWithOptionsAndDefaults can set these programmatically?
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #3108 +/- ##
==========================================
+ Coverage 75.99% 76.02% +0.04%
==========================================
Files 566 567 +1
Lines 64277 64370 +93
==========================================
+ Hits 48840 48930 +90
- Misses 11764 11767 +3
Partials 3673 3673 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
Addressing miparnisari's review — added OTelConfig as a struct in the server package with an OTel field on Config so it can be set programmatically without going through Cobra flags. InitOTelProvider now takes that struct directly along with a context and has no Cobra dependency at all. |
Fixes authzed#712 and authzed#3095. - Remove dependency on github.com/jzelinskie/cobrautil/v2/cobraotel - Replicate OTel provider initialization natively in pkg/cmd/server/otel.go - Wire TracerProvider into serve.go signal handler so Shutdown and ForceFlush are called on SIGTERM/SIGINT, preventing span loss on exit - Fix vendored cobrautil Viper global singleton bug: viper.SetEnvPrefix was mutating global state instead of the local instance (v.SetEnvPrefix) - Touch pkg/cmd/util/util.go only to break import cycle between pkg/cmd/util and pkg/cmd/server; all flag registrations unchanged - Add 20 tests across unit, integration, and system build tags
- use ctxkey package for context key instead of a bare struct type - drop legacy otel-jaeger-* flags - collapse ShutdownOTelProvider to a single context shared across ForceFlush and Shutdown - move OTel initialization out of the Cobra layer into Config.Complete via an OTelConfig struct, matching the pattern used by other components - register provider shutdown with closeables so it participates in the ordered server shutdown sequence - add env var precedence tests
edc506d to
7a2514c
Compare
There was a problem hiding this comment.
Do we really need three different files for tests?
EDIT: I've simplified this
| --otel-endpoint string OpenTelemetry collector endpoint - the endpoint can also be set by using enviroment variables | ||
| --otel-insecure connect to the OpenTelemetry collector in plaintext | ||
| --otel-provider string OpenTelemetry provider for tracing ("none", "otlphttp", "otlpgrpc") (default "none") | ||
| --otel-sample-ratio float ratio of traces that are sampled (default 0.01) | ||
| --otel-service-name string service name for trace data (default "spicedb") | ||
| --otel-trace-propagator string OpenTelemetry trace propagation format ("b3", "w3c", "ottrace"). Add multiple propagators separated by comma. (default "w3c") |
There was a problem hiding this comment.
FYI - serve still has them. All the other commands don't use these values.
tstirrat15
left a comment
There was a problem hiding this comment.
See comment, otherwise LGTM
Description
Closes #3095 and #712
Testing
docker-compose up --build, then send a few requests to the server and go to localhost:3000. You should be able to see traces